package com.tz.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {
	private static final SessionFactory sessionFactory = buildSessionFactory();

	private static SessionFactory buildSessionFactory() {
		try {
			Configuration cfg = new Configuration().configure();
			//实例化标准的服务注册处构建器
			StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder();
			//套用配置信息
			ssrb.applySettings(cfg.getProperties());
			//构建一个标准服务注册处
			ServiceRegistry sr = ssrb.build();
			//使用服务注册处来构建一个已注册的SessionFactory(一项Hibernate重要的服务)
			return cfg.buildSessionFactory(sr);
		} catch (Throwable ex) {
			System.err.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}

}
